package ema.tpjee.banking.session;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import session.BankingSessionFacadeRemote;
import session.SessionFacadeRemote;
import entity.Auteur;
import entity.Bank;
import entity.Customer;
import entity.Livre;

/**
 * Simple client of the stateless.
 * 
 * @author pfister
 */
public final class BankingDelegate implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -861746215317587187L;

	public String getDummy(){
		return "ok";
	}
	
	public void setDummy(String s){
		
	}
	
	
	public BankingDelegate() {
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");

		BankingSessionFacadeRemote facadeBean;
		try {
			facadeBean = (BankingSessionFacadeRemote) new InitialContext()
					.lookup("tplibEAR/BankingSessionFacade/remote");

			// Initialisation de la base
			facadeBean.populate();

			List<Bank> banks = facadeBean.getBanks();
			if (banks != null) {
				for (Bank bank : banks) {
					System.out.println("Liste des clients pour la banque "
							+ bank.getName() + " :");
					Collection<Customer> customers = bank.getCustomers();
					if (customers == null) {
						System.out.println(" Aucun client");
					} else {
						for (Customer customer : customers)
							System.out.println("    client: "
									+ customer.getName());
					}
				}
			} else {
				System.out.println("Aucune banque");
			}

			// Recuperation de la liste des Customers
			System.out.println("");
			System.out.println("Liste des clients + banque :");
			List<Customer> customers = facadeBean.getCustomers();
			if (customers != null) {
				for (Customer Customer : customers) {
					System.out.println("client: " + Customer.getName()
							+ " /banque: " + Customer.getBank().getName());
				}
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
